//
//  ViewController.m
//  UISegmentControl
//
//  Created by 王恕 on 15/12/10.
//  Copyright © 2015年 王恕. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    UISegmentedControl* Segmented;

}

@property (nonatomic,weak) UIView *firstview;
@property (nonatomic,weak) UIView *secondview;
@property (nonatomic,weak) UIView *thirdview;

@end

@implementation ViewController

-(UIView *)firstview {
    if (_firstview == nil) {
       UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 40, self.view.bounds.size.width, self.view.bounds.size.height);
        view.backgroundColor = [UIColor redColor];
        _firstview = view;
        
        [self.view addSubview:_firstview];
    }
    return _firstview;
}

-(UIView *)secondview{
    if (_secondview == nil) {
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 40, self.view.bounds.size.width, self.view.bounds.size.height - 100);
        view.backgroundColor = [UIColor blueColor];
        _secondview = view;
        
        [self.view addSubview:_secondview];
    }
    return _secondview;
}

-(UIView *)thirdview{
    if (_thirdview == nil) {
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 40, self.view.bounds.size.width, self.view.bounds.size.height - 200);
        view.backgroundColor = [UIColor orangeColor];
        _thirdview = view;
        [self.view addSubview:_thirdview];
    }
    return _thirdview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建Segmented
    NSArray* segmenteArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4", nil];
    Segmented = [[UISegmentedControl alloc]initWithItems:segmenteArray];//设置段数
    Segmented.frame = CGRectMake(0, 20, self.view.bounds.size.width, 20);//设置位置
    
    //设置属性
    [Segmented setTitle:@"one" forSegmentAtIndex:0];//将第一段的标题设置为one
    [Segmented setImage:[UIImage imageNamed:@"1.jpg"] forSegmentAtIndex:1];//用1的图片为第二段命名
    [Segmented insertSegmentWithImage:[UIImage imageNamed:@"2.jpg"] atIndex:2 animated:NO];
    [Segmented insertSegmentWithTitle:@"insert" atIndex:1 animated:NO];//指定位置插入索引
    [Segmented removeSegmentAtIndex:0 animated:NO];//移除指定位置的索引
    [Segmented setWidth:10.0f forSegmentAtIndex:4];//设置指定索引的宽度
  //  Segmented.selectedSegmentIndex =0;//设置默认选择项索引
    Segmented.tintColor = [UIColor grayColor];
    Segmented.momentary = NO;//是否保持选中状态
    

    [Segmented addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:Segmented];
    
    Segmented.selectedSegmentIndex = 0;
    
   [self click:Segmented];
    
}

- (void)click: (id)sender{

    
    
  //  CGRect  rect = CGRectMake(0, 40, self.view.bounds.size.width, self.view.bounds.size.height);
    switch (Segmented.selectedSegmentIndex) {
        case 0:{
            [_secondview removeFromSuperview];
            [_thirdview removeFromSuperview];
            [self firstview];
            NSLog(@"1");
        }
            break;
        case 1:{
            [self.firstview removeFromSuperview];
            [self.thirdview removeFromSuperview];
            [self secondview];
        }
            break;
        case 2:{
            [self.firstview removeFromSuperview];
            [self.secondview removeFromSuperview];
            [self thirdview];
            NSLog(@"3");
        }
            break;
        case 3:{
            [self.firstview removeFromSuperview];
            [self.secondview removeFromSuperview];
            [self.thirdview removeFromSuperview];
            NSLog(@"4");
        }
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
